/**
 * @file   EventPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-26
 * @brief  DON packet object: Event
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/EventPacket.h>
#include <DON/Core/Packet/PacketDefines.h>
#include <DON/Core/DataType/UInt16.h>
#include <DON/Core/DataType/Boolean.h>
#include <QtCore/QByteArray>
#include <QtCore/QList>

#define MAX_EVENT_VALUE_COUNT    (DON_CORE_PACKET_MAX_PAYLOAD_DATA_ITEM_COUNT - 2)

using namespace DON::Core::Packet;
using namespace DON::Core::DataType;

class EventPacket::Private
{
public:
    Private()
        : eventId(0u),
          eventValueList(QList<const AbstractDataType *>())
    { }

    ~Private()
    {
        removeAllEventValues();
    }

    void removeAllEventValues();

    quint16 eventId;
    bool confirmationRequired;
    QList<const AbstractDataType *> eventValueList;
};

EventPacket::EventPacket()
    : AbstractPacket(),
      d(new Private())
{
    setPacketType(PacketType_Event);
}

EventPacket::EventPacket(const quint16 source,
                         const quint16 destination,
                         const quint16 sequence,
                         const quint16 eventId,
                         const bool confirmationRequired)
    : AbstractPacket(source,
                     destination,
                     PacketType_Event,
                     sequence),
      d(new Private())
{
    d->eventId = eventId;
    d->confirmationRequired = confirmationRequired;
}

EventPacket::EventPacket(const EventPacket &other)
    : AbstractPacket(other),
      d(new Private())
{
    d->eventId = other.d->eventId;
    d->confirmationRequired = other.d->confirmationRequired;

    const int size = other.d->eventValueList.size();

    for (int index = 0; index < size; ++index)
    {
        const AbstractDataType *eventValue = other.d->eventValueList.at(index);

        if (eventValue != NULL)
        {
            d->eventValueList.append(eventValue->clone());
        }
    }
}

EventPacket::~EventPacket()
{
    delete d;
}

EventPacket & EventPacket::operator=(const EventPacket &other)
{
    AbstractPacket::operator=(other);

    d->eventId = other.d->eventId;
    d->confirmationRequired = other.d->confirmationRequired;

    d->removeAllEventValues();

    const int size = other.d->eventValueList.size();

    for (int index = 0; index < size; ++index)
    {
        const AbstractDataType *eventValue = other.d->eventValueList.at(index);

        if (eventValue != NULL)
        {
            d->eventValueList.append(eventValue->clone());
        }
    }

    return *this;
}

bool EventPacket::isValid() const
{
    bool valid = false;

    if (d->eventValueList.size() <= MAX_EVENT_VALUE_COUNT)
    {
        valid = true;
        const int size = d->eventValueList.size();

        for (int index = 0; index < size; ++index)
        {
            const AbstractDataType *eventValue = d->eventValueList.at(index);

            if (eventValue != NULL)
            {
                valid = isDataTypeValid(eventValue->getDataType());
            }
            else
            {
                valid = false;
            }

            if (valid == false)
            {
                break;
            }
        }
    }

    return valid;
}

quint16 EventPacket::getEventId() const
{
    return d->eventId;
}

void EventPacket::setEventId(const quint16 eventId)
{
    d->eventId = eventId;
}

bool EventPacket::getConfirmationRequired() const
{
    return d->confirmationRequired;
}

void EventPacket::setConfirmationRequired(const bool confirmationRequired)
{
    d->confirmationRequired = confirmationRequired;
}

int EventPacket::getEventValueCount() const
{
    return d->eventValueList.size();
}

const AbstractDataType * EventPacket::getEventValue(const int index) const
{
    const AbstractDataType *eventValue = NULL;

    if ((index >= 0) &&
        (index < d->eventValueList.size()))
    {
        eventValue = d->eventValueList.at(index);
    }

    return eventValue;
}

bool EventPacket::setEventValue(const AbstractDataType &eventValue, const int index)
{
    bool success = false;

    if ((index >= 0) &&
        (index < d->eventValueList.size()))
    {
        delete d->eventValueList[index];

        d->eventValueList[index] = eventValue.clone();

        if (d->eventValueList.at(index) != NULL)
        {
            success = true;
        }
    }

    return success;
}

bool EventPacket::addEventValue(const AbstractDataType &eventValue)
{
    bool success = false;

    if (d->eventValueList.size() < MAX_EVENT_VALUE_COUNT)
    {
        d->eventValueList.append(eventValue.clone());

        if (d->eventValueList.last() != NULL)
        {
            success = true;
        }
    }

    return success;
}

bool EventPacket::removeEventValue(const int index) const
{
    bool success = false;

    if ((index >= 0) &&
        (index < d->eventValueList.size()))
    {
        delete d->eventValueList.takeAt(index);
        success = true;
    }

    return success;
}

void EventPacket::removeAllEventValues()
{
    d->removeAllEventValues();
}

int EventPacket::getMaxEventValueCount()
{
    return MAX_EVENT_VALUE_COUNT;
}

QList<QByteArray> EventPacket::getPayloadDataItems(bool *ok) const
{
    QList<QByteArray> payloadDataItems;
    bool success = false;

    if (isValid())
    {
        success = true;

        // Event ID
        payloadDataItems.append(UInt16::toBinary(d->eventId));

        // Confirmation Required
        const char confirmationRequired = Boolean::toBinary(d->confirmationRequired);
        payloadDataItems.append(QByteArray(1, confirmationRequired));

        // Event Value List
        const int size = d->eventValueList.size();

        for (int index = 0; index < size; ++index)
        {
            // Event value
            const AbstractDataType *eventValue = d->eventValueList.at(index);
            const char dataTypeData = fromDataTypeToBinary(eventValue->getDataType());
            const QByteArray valueData = eventValue->toBinary();

            if (valueData.isEmpty())
            {
                // Error
                payloadDataItems.clear();
                success = false;
            }
            else
            {
                // Add event value to the list
                QByteArray eventValueData;
                eventValueData.append(dataTypeData);
                eventValueData.append(valueData);

                payloadDataItems.append(eventValueData);
            }
        }
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return payloadDataItems;
}

void EventPacket::Private::removeAllEventValues()
{
    const int size = eventValueList.size();

    for (int index = 0; index < size; ++index)
    {
        delete eventValueList.at(index);
    }

    eventValueList.clear();
}
